/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.pucp.Simulacion.Vista;

import java.awt.Component;
import java.awt.GridLayout;
import java.util.ArrayList;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import pe.edu.pucp.Maestros.DAO.AeropuertoDAO;
import pe.edu.pucp.Maestros.DAO.MaestoVueloDAO;
import pe.edu.pucp.Modelo.TipoAvion;
import pe.edu.pucp.Modelo.Vuelo;
import pe.edu.pucp.Simulacion.Interfaz.AmpliacionVuelo;
import pe.edu.pucp.Simulacion.Interfaz.NecesidadesVuelos;

/**
 *
 * @author katy
 */
public class TablaVuelos extends javax.swing.JPanel {
    private NecesidadesVuelos necesidadesVuelos;
    private ArrayList<TipoAvion> lstTipo;
    private ArrayList<Vuelo> lstVuelos;
    private JTable tabla;
    
    public TablaVuelos(ArrayList<Vuelo> listaVuelos) {
        super(new GridLayout(1,0));
        //Get BD Data
        lstTipo = (new AeropuertoDAO()).listarTipoAviones();
        if (listaVuelos == null || listaVuelos.size() == 0)
            lstVuelos = (new MaestoVueloDAO()).ListarVuelos();
        else 
            lstVuelos = listaVuelos;
        necesidadesVuelos = new NecesidadesVuelos(lstVuelos);
        
        //Set up the editor for the sport cells.
        JComboBox comboBox = new JComboBox();
        for(int i=0; i < lstTipo.size();i++) {
            comboBox.addItem(lstTipo.get(i).getCappack());
        }
        tabla = new JTable(new TablaVuelos.MyTableModel(lstTipo, necesidadesVuelos, lstVuelos,comboBox) );
//        table.setPreferredScrollableViewportSize(new Dimension(408, 200));
        tabla.setFillsViewportHeight(true);
 
        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(tabla);
 
        //Set up column sizes.
        initColumnSizes(tabla);
 
        //Fiddle with the Sport column's cell editors/renderers.
        setUpSportColumn(tabla, tabla.getColumnModel().getColumn(3), lstTipo, comboBox);
        
        //Add the scroll pane to this panel.
        add(scrollPane);
    }
 
    public JTable getTablaVuelos() {
        return this.tabla;
    }
    
    public NecesidadesVuelos getNecesidadesVuelos() {
        return necesidadesVuelos;
    }
    
    public ArrayList<Vuelo> getLstVuelos() {
        return this.lstVuelos;
    }
    /*
     * This method picks good column sizes.
     * If all column heads are wider than the column's cells'
     * contents, then you can just use column.sizeWidthToFit().
     */
    private void initColumnSizes(JTable table) {
        TablaVuelos.MyTableModel model = (TablaVuelos.MyTableModel)table.getModel();
        TableColumn column;
        Component comp;
        int headerWidth;
        int cellWidth;
        Object[] longValues = model.longValues;
        TableCellRenderer headerRenderer =
            table.getTableHeader().getDefaultRenderer();
 
        for (int i = 0; i < 4; i++) {
            column = table.getColumnModel().getColumn(i);
 
            comp = headerRenderer.getTableCellRendererComponent(
                                 null, column.getHeaderValue(),
                                 false, false, 0, 0);
            headerWidth = comp.getPreferredSize().width;
 
            comp = table.getDefaultRenderer(model.getColumnClass(i)).
                             getTableCellRendererComponent(
                                 table, longValues[i],
                                 false, false, 0, i);
            cellWidth = comp.getPreferredSize().width;
            column.setPreferredWidth(Math.max(headerWidth, cellWidth));
        }
    }
 
    private void setUpSportColumn(JTable table,
                                 TableColumn sportColumn, ArrayList <TipoAvion> lstTipo, JComboBox comboBox ) {
        
        sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
      
        //Set up tool tips for the sport cells.
        DefaultTableCellRenderer renderer =
                new DefaultTableCellRenderer();
//        renderer.setToolTipText("Click for combo box");
        sportColumn.setCellRenderer(renderer);
    }
 
    class MyTableModel extends AbstractTableModel {
        private ArrayList<TipoAvion> lstTipo;
        private JComboBox combo;
        private String[] columnNames = {"ORIGEN","DESTINO", "CAPACIDAD","AMPLIACION"};
        private ArrayList<Vuelo> data;
        private NecesidadesVuelos necesidades; 
        public final Object[] longValues = {"ORIGEN", "DESTINO", "CAPACIDAD", new Integer(4)};
 
        public MyTableModel(ArrayList<TipoAvion> lstTipo, NecesidadesVuelos vuelos, ArrayList<Vuelo> lstVuelo,JComboBox combo) 
        {
            super();
            this.lstTipo = lstTipo;
            this.combo = combo;
            this.data = lstVuelo;
            this.necesidades = vuelos;
        } 
        
        @Override
        public int getColumnCount() {
            return columnNames.length;
        }
 
        @Override
        public int getRowCount() {
            return data.size();
        }
 
        @Override
        public String getColumnName(int col) {
            return columnNames[col];
        }
 
        @Override
        public Object getValueAt(int row, int col) {
            Object o = new Object();
            Vuelo v = data.get(row);
            AmpliacionVuelo ampliacion = necesidades.get(row);
            TipoAvion t = new TipoAvion();
            for (int i=0; i<lstTipo.size();i++) {
                if (lstTipo.get(i).getIdtipo() == ampliacion.getTipoAvionFinalId()){
                    t = lstTipo.get(i);
                    break;
                }
            }
         
            switch (col) 
            {
                case 0: 
                    o = v.getAeropuertoByIdorigen().getCiudad().getDescripcion();
                    break;
                case 1:
                    o = v.getAeropuertoByIddestino().getCiudad().getDescripcion();
                    break;
                case 2:
                    o = v.getAvion().getTipoAvion().getCappack();
                    break;
                case 3:
                    o = t.getCappack();
                    break;
            }
            return o;
        }
 
        /*
         * JTable uses this method to determine the default renderer/
         * editor for each cell.  If we didn't implement this method,
         * then the last column would contain text ("true"/"false"),
         * rather than a check box.
         */
        @Override
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }
 
        /*
         * Don't need to implement this method unless your table's
         * editable.
         */
        @Override
        public boolean isCellEditable(int row, int col) {
            if (col < 2) {
                return false;
            } else {
                return true;
            }
        }
 
        /*
         * Don't need to implement this method unless your table's
         * data can change.
         */
        @Override
        public void setValueAt(Object value, int row, int col) {
            TipoAvion t = new TipoAvion();
            for (int i=0; i<lstTipo.size();i++) {
                if (lstTipo.get(i).getCappack() == (int)value){
                    t = lstTipo.get(i);
                    break;
                }
            }
            
            Object o = new Object();
            AmpliacionVuelo v = necesidades.get(row);
            v.setTipoAvionFinalId(t.getIdtipo());
            fireTableCellUpdated(row, col);
        }
    }
 
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
